const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    proList:[],
    more:true,
    moveData1:null,
    moveData2:null,
    inputShowed:false,
    inputVal:"",
    masking:false,
    isHideLoadMore: true,
    page:1,
    key:'',
    ProductStyleId: null,
    ProductTypeId: '',
    houseid: null,
    image: '../images/screen.png',
    maskList:[],
    rukupage: false,
    offerpage: false,
    chukepage: false
  },
  changeText: function (e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
  },
  moreClick:function(){
    console.log(this.data.more)
    var animation1 = wx.createAnimation({
                      duration: 3000,
                      delay: 0,
                      timingFunction: "ease",
                    })
    var animation2 = wx.createAnimation({
                      duration: 3000,
                      delay: 0,
                      timingFunction: "ease",
                    })
    if(this.data.more){
      animation1.translate(-55, 0).step({ duration: 1000 })
      animation2.translate(0, -55).step({ duration: 1000 })
    } else {
      animation1.translate(0, 0).step({ duration: 1000 })
      animation2.translate(0, 0).step({ duration: 1000 })
    }
    this.setData({
      more:!this.data.more,
      moveData1: animation1.export(),
      moveData2: animation2.export()
      // scaleData: animation.export()
    })
  },
  clickScreen: function(){
    if (this.data.masking){
      this.setData({
        masking: false,
        image: '../images/screen.png'
      })
    }else{
      this.setData({
        masking: true,
        image: '../images/screen-active.png'
      })
    }
  },
  sltScn: function(e){
    console.log(e)
    var id = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : null
    this.setData({
      page: 1,
      ProductTypeId: id
    })
    this.getList()
  },
  search: function () {
    this.setData({
      page: 1
    })
    this.getList()
  },
  open: function (e) {
    console.log(this.data.rukupage)
    var id = e.currentTarget.dataset.id
    var that = this
    var arr = []
    if (this.data.rukupage || this.data.offerpage){
      arr = ['预览', '修改', '删除产品', '选择产品']
    }else{
      arr = ['预览', '修改', '删除产品']
    }
    wx.showActionSheet({
      itemList: arr,
      success: function (res) {
        console.log(res)
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            wx.navigateTo({
              url: './addPro/addPro?create=1&&id=' + id
            })
          } else if (res.tapIndex == 1) {
            wx.navigateTo({
              url: './addPro/addPro?id=' + id
            })
          } else if (res.tapIndex == 2) {
            wx.showModal({
              title: '删除产品',
              content: '你确定删除该产品吗？',
              confirmText: "删除",
              cancelText: "取消",
              success: function (res) {
                console.log(res);
                if (res.confirm) {
                  console.log('删除')
                  app.req({
                    url:"HouseStore/Product/"+id,
                    method: "DELETE",
                    success: function(res){
                      if(res.data.Code){
                        wx.showToast({
                          title: '删除成功',
                          icon: 'success',
                          duration: 1500
                        });
                        wx.startPullDownRefresh()
                      }else{
                        wx.showToast({
                          title: '删除失败',
                          duration: 1500
                        });
                      }
                    }
                  })
                } else {
                  console.log('取消')
                }
              }
            });
          } else if (res.tapIndex == 3) {
            if(that.data.rukupage){
              let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
              let prevPage = pages[pages.length - 2];
              var obj = e.currentTarget.dataset.item
              obj.Quantity = 1
              var arr = prevPage.data.proList
              arr.push(obj)
              prevPage.setData({
                proList: arr
              })
              wx.navigateBack({
                delta: 1
              })
            }else if(that.data.offerpage){
              let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
              let prevPage = pages[pages.length - 2];
              var obj = e.currentTarget.dataset.item
              obj.Price = 0
              obj.Quantity = 1
              obj.remark = ''
              var arr = prevPage.data.proList
              arr.push(obj)
              prevPage.setData({
                proList: arr
              })
              wx.navigateBack({
                delta: 1
              })
            }
            
          }
        }
      }
    })
  },
  getList: function(){
    var that=this
    app.req({
      url: 'Product',
      data: {
        pageindex: this.data.page,
        pagesize: 15,
        ProductTypeId: this.data.ProductTypeId,
        key:this.data.key
      },
      success: function (res) {
        if (that.data.page == 1) {
          that.setData({
            proList: res.data.result
          })
        } else if (that.data.page > 1) {
          that.setData({
            proList: that.data.proList.concat(res.data.result)
          })
        }
        if (res.data.result.length == 0){
          that.setData({
            page:that.data.page-1
          })
        }
        setTimeout(function(){
          wx.stopPullDownRefresh()
        },1000)
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that=this
    if(options.type == 2){
      this.setData({
        rukupage: true
      })
    }else if(options.type == 3){
      this.setData({
        offerpage: true
      })
    }else if(options.type == 4){
      this.setData({
        chukepage: true,
        houseid: options.houseid
      })
    }
    app.req({
      url: "SystemConfig/ProductType",
      success: function(res){
        that.setData({
          maskList:res.data.result
        })
      }
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.startPullDownRefresh({
      
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多')
    this.setData({
      isHideLoadMore: false,
    })
    this.setData({
      page: this.data.page+1
    })
    this.getList()
    setTimeout(() => {
      this.setData({
        isHideLoadMore: true,
      })
    }, 1000)
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})